package com.xiaoke.liantongdemo.xml;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.xiaoke.liantongdemo.bean.Business;
import com.xiaoke.liantongdemo.bean.Category;
import com.xiaoke.liantongdemo.bean.Group;

public class GroupHandler extends DefaultHandler {
	
	private Group group;
	private Business business;
	private Category category;
	private ArrayList<Category> categories;
	private ArrayList<Business> businesses;
	
	private final static String GROUP = "group";
	private final static String GROUP_SUMMARY = "group_summary";
	private final static String GROUP_DETAIL = "group_detail";
	private final static String CATEGORYS = "categorys";
	private final static String CATEGORY = "category";
	private final static String CATEGORY_NAME = "category_name";
	private final static String BUSINESSES = "businesses";
	private final static String BUSINESS = "business";
	private final static String ID = "id";
	private final static String NAME = "name";

	public Group getGroup(){
		return group;
	}


	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (GROUP.equals(localName)) {
			group.setCategorys(categories);
		} else if (CATEGORY.equals(localName)) {
			category.setBusinesses(businesses);
			categories.add(category);
		}
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (BUSINESS.equals(localName)) {
			business = new Business(Integer.parseInt(attributes.getValue(ID)),
					attributes.getValue(NAME));
			businesses.add(business);
		} else if (BUSINESSES.equals(localName)) {
			businesses = new ArrayList<Business>();
		} else if (CATEGORY.equals(localName)) {
			category = new Category();
			category.setName(attributes.getValue(NAME));
		} else if (CATEGORYS.equals(localName)) {
			categories = new ArrayList<Category>();
		} else if (GROUP.equals(localName)) {
			group = new Group();
			group.setName(attributes.getValue(NAME));
		}

	}

}
